package controllers;

import java.util.Date;
import java.util.List;

import play.*;
import play.mvc.*;

import models.*;

@With(Secure.class)
public class Agents extends CRUD {  
	@Before
	static void setConnectedUser() {
		if(Security.isConnected()) {
			Agent agent = Agent.find("byEmail", Security.connected()).first();
			renderArgs.put("agent_email", agent.email);
			renderArgs.put("agent_fullname", agent.fullname());
			renderArgs.put("agent_respPedago", SystemSDIS.getSystemSDIS().isResponsableDepartemental(agent));
			renderArgs.put("agent_orga", !agent.isOrgaStage(agent));
			renderArgs.put("resp", (agent.isOrgaStage(agent) || SystemSDIS.getSystemSDIS().isResponsableDepartemental(agent)));
			if(agent.niv.id != 0){
				renderArgs.put("agent_for", agent.niv);
			}
		}
	}

	public static void monProfil(){
		Agent agent = Agent.find("byEmail", Security.connected()).first();
		render(agent);
	}

	public static void monProfilSave(String nom, String prenom, Date dateNais, String email, String password){
		Agent agent = Agent.find("byEmail", Security.connected()).first();
		agent.nom = nom;
		agent.prenom = prenom;
		agent.dateNais = dateNais;
		agent.email = email;
		agent.password = password;
		validation.valid(agent);

		if(validation.hasErrors()){
			flash.error("Veuillez saisir tous les champs.");
			render("@monProfil", agent);
		}			
		else{
			agent.save();
			flash.success("Vos modifications ont bien été enregistré.");
			monProfil();
		}
	}
}